package com.tanger.common.sql;

import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import java.sql.*;
import java.util.List;

/**
 * 参数化查询工具
 */
public class DBHelper {
   
	private String sql;

	//要传入的sql语句    
	public void setSql(String sql) {
		this.sql = sql;
	}
  
	private List sqlValues;  

	//sql语句的参数
	public void setSqlValues(List sqlValues) {
		this.sqlValues = sqlValues;
	}

	//连接对象
	private Connection con;  
	
	public void setCon(Connection con) {
		this.con = con;
	}

	public DBHelper(){
		//给Connection的对象赋初值
		this.con=getConnection();  
	}
	
	/**
	 * 获取数据库连接
	 * @return
	 */
    private  Connection getConnection(){ 
    	try {
			String driver_class=DataResourceProp.driver;
			String driver_url=DataResourceProp.dataResourceUrl;
			String database_user=DataResourceProp.visiUserName;
			String database_password=DataResourceProp.visiPassword;

			Class.forName(driver_class);
			con=DriverManager.getConnection(driver_url,database_user,database_password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
    }
    
  
    

    /**
                * 关闭数据库
     * @param con
     * @param pst
     * @param rst
     */
    private  void closeAll(Connection con,PreparedStatement pst,ResultSet rst){
    	if(rst!=null){
    		try {
				rst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
    	}
    	
    	if(pst!=null){
    		try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
    	}
    	
    	if(con!=null){
    		try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
    	}
    	
    	
    }
    
    /**
     * 查找
     * @return
     */
    public Result executeQuery(){
    	Result result=null;
    	ResultSet rst=null;
    	PreparedStatement pst=null;
    	try {
    	
			pst=con.prepareStatement(sql);
			if(sqlValues!=null&&sqlValues.size()>0){  //当sql语句中存在占位符时
				setSqlValues(pst,sqlValues);
			}
	    rst=pst.executeQuery();
	    result= ResultSupport.toResult(rst);  //一定要在关闭数据库之前完成转换
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(con, pst, rst);
		}
		
    	return result;
    }
    
    
    /**
     * 增删改
     * @return
     */
    public int executeUpdate(){
    	int result=-1;
    	PreparedStatement pst=null;
        try {
			pst=con.prepareStatement(sql);
			if(sqlValues!=null && sqlValues.size()>0){  //当sql语句中存在占位符时
				setSqlValues(pst,sqlValues);
			}
		result=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(con, pst, null);
		}
    
    	
    	return result;
    }
    
    
    
    /**
     * 给sql语句中的占位符赋值
     * @param pst
     * @param sqlValues
     */
    private void setSqlValues(PreparedStatement pst,List sqlValues){
    	for(int i=0;i<sqlValues.size();i++){
    		try {
				pst.setObject(i+1,sqlValues.get(i));
			} catch (SQLException e) {
				e.printStackTrace();
			}
    	}
    }
}